package com.weitu.permission.district.controller;

import com.weitu.base.controller.BaseController;
import com.weitu.permission.district.entity.Country;
import com.weitu.permission.district.service.CityService;
import com.weitu.permission.district.service.CountryService;
import com.weitu.permission.district.service.CountyService;
import com.weitu.permission.district.service.ProvinceService;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LiMeng 2017/4/7
 */
@Controller
@RequestMapping(value = "permission/district")
public class DistrictController extends BaseController {
    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "permission/district";
    private final CountryService countryService;
    private final ProvinceService provinceService;
    private final CityService cityService;
    private final CountyService countyService;

    @Autowired
    public DistrictController(ProvinceService provinceService, CityService cityService, CountyService countyService, CountryService countryService) {
        this.provinceService = provinceService;
        this.cityService = cityService;
        this.countyService = countyService;
        this.countryService = countryService;
    }

    @ResponseBody
    @RequestMapping(value = "/country")
    public List countryAjax(String name, String code) {
        if (StringUtils.isNotEmpty(code)) {//有code忽略名称查询
            List<Country> countryList = new ArrayList<Country>();
            countryList.add(countryService.selectByPrimaryKey(code));
            return countryList;
        } else if (StringUtils.isEmpty(name)) {
            return countryService.queryList();
        } else {
            return countryService.queryListByName(name);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/province")
    public List provinceAjax(String code) {
        return provinceService.queryList(code);
    }

    @ResponseBody
    @RequestMapping(value = "/city")
    public List cityAjax(String parentCode, String code) {
        if (StringUtils.isEmpty(parentCode)) {
            return ListUtils.EMPTY_LIST;
        }
        return cityService.queryList(parentCode, code);
    }

    @ResponseBody
    @RequestMapping(value = "/county")
    public List countyAjax(String parentCode, String code) {
        if (StringUtils.isEmpty(parentCode)) {
            return ListUtils.EMPTY_LIST;
        }
        return countyService.queryList(parentCode, code);
    }
}
